function isString(input){
    return typeof(input) == 'string' && isNaN(input)
}
function validEmail(emailElement){
    var emailExpression = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    return emailExpression.test(emailElement);
    
}

$(function(){
    $('.error').hide();
    $('input.userInput').css({
        backgroundColor:"FFFFFF"
    });
    $('input.userInput').focus(function(){
        $(this).css({
            backgroundColor:"#FFCCCC"
        });
    });
    $('input.userInput').blur(function(){
        $(this).css({
            backgroundColor:"#FFFFFF"
        });
    });
    $(".button").click(function(){
        $('.error').hide();
        var otherName = $("input#otherName").val();
        var userType = $("select#userType").val();
        if(userType == ""){
            $("span#userTypeError").show();
            $("select#userType").focus();
           
            return false;
        }
        var firstName = $("input#firstName").val();
        if(firstName == "" || firstName.length < 1 || !(isString(firstName))){
            $("span#firstNameError").show();
            $("input#firstName").focus();
            
            return false; 
        }
        var lastName = $("input#lastName").val();
        if(lastName == "" || lastName.length < 2 || !(isString(lastName))){
            $("span#lastNameError").show();
            $("input#lastName").focus();
            
            return false;
        }
        var dateOfBirth = $("input#dob").val();
        if(dateOfBirth == "" || dateOfBirth.length == 0){
            $("span#dobError").show();
            $("input#dob").focus();
            
            return false;
        }
        var email = $("input#email").val();
        if(email == "" || !(validEmail(email))){
            $("span#emailError").show();
            $("input#email").focus();

            return false;
        }
        
        var password = $("input#password").val();
        if(password == "" ||password.length < 6){
            $("span#passwordError").show();
            $("input#password").focus();
            
            return false;
        }
        
        var address = $("textarea#address").val();
        if(address == ""){
            $("span#addressError").show();
            $("textarea#address").focus();
            return false;
        }
        var allData = '&userType='+ userType + '&firstName=' + firstName +'&lastName='  + lastName + '&otherName='  + otherName +'&dob=' + dateOfBirth + 
        '&gender=' + gender + '&regions=' + region + '&schName='  + schoolName + '&email=' + email +
        '&password=' + password + '&pin=' + pin + '&pinSerial=' + pinSerial;
        $.ajax({
            type: "POST",
            url: "./add_user.jsp",
            //            url: "./validateLogin.php",
            //            data: allData,
            data: {
                firstName :  $("input#firstName").val(),
                lastName  :  $("input#lastName").val(),
                otherName  :  $("input#otherName").val(),
                dateOfBirth : $("input#dob").val(),
                email : $("input#email").val(),
                password : $("input#password").val(),
                address : $("textarea#address").val()
            },

            success: function(){
                console.log(" First Name: " +firstName+" Last Name: "+lastName+ " Date of Birth:"+dateOfBirth
                    +" gender: " +gender+" School name: " +schoolName+ " Region: " +region +"email: "+email+" passsword: "+password
                    +" pin : "+pin+" pin serial : "+pinSerial);

                $('#signInForm').html("<div id='message'></div>");
                $('#message').html("<h2>Registration Form Submitted!</h2>")
                $('#message').html("<h3>Your form has been submitted successfully!!!</h3>")
                .append("<a href='./SignIn.php'><p>You May Login</p></a>")
                .hide()
                .fadeIn(1500, function(){
                    $('#message').append("<img id='checkmark' src='./images/tick.png' />")
                });
            }
        })
    });
});